<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>优先级队列结构</title>
    特点：
    和普通队列结构的区别在于进入队列时是按照优先级进入的(优先级越高，越靠前)
</head>

<body>
    <script>
        class PriorityQueue {
            constructor() {
                this.items = []
                class PriorityElement {
                    constructor(element, priority) {
                        this.element = element
                        this.priority = priority
                    }
                }
            }
            enQueue(element, priority) {
                const priorityElement = new PriorityElement(element, priority)
                let isInsert = false
                for (let i = 0; i < this.items.length; i++) {
                    if (priorityElement.priority > this.items[i].priority) {
                        this.items.splice(i, 0, priorityElement)
                        isInsert = true
                        break
                    }
                }
                if (!isInsert) {
                    this.items.push(priorityElement)
                }
            }
            deQueue() {
                return this.items.shift()
            }
            front() {
                return this.items[0]
            }
            isEmpty() {
                return this.items.length == 0
            }
            size() {
                return this.items.length
            }
            toString() {
                var resultStr = ''
                for (let i = 0; i < this.items.length; i++) {
                    resultStr += `${this.items[i].element}:${this.items[i].priority} `
                }
                return resultStr
            }
        }

        function getPriorityQueueContent() {
            let queue = new PriorityQueue()
            queue.enQueue('abc', 1)
            queue.enQueue('cba', 10)
            queue.enQueue('nbc', 0.2)
            queue.enQueue('mbc', -1)
            return queue.toString()
        }
        console.log(getPriorityQueueContent())
    </script>
</body>

</html>